﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OCR.Function;
using OCR.SystemThread;

namespace OCR.SettingsForm
{
    public partial class PCI1616IO_Form : Form
    {
        public PCI1616IO_Form()
        {
            InitializeComponent();
        }
        private void PCI1616IO_Load(object sender, EventArgs e)
        {
            PCI1616_Initial.PCI1616_InitialClass();
            if (PCI1616_Initial.iResult == 0)
            {
                listBox1.Items.Add("DLL版本号为：" + Encoding.Default.GetString(PCI1616_Initial.DLLVer).Replace("\0", ""));
                listBox1.Items.Add("卡号:" + PCI1616_Initial.cardId + "句柄：" + PCI1616_Initial.hDevice);
            }
            else { }

            //IOThread_Run.DIO_State();
            timer1.Start();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)
            {
                //IOThread_Run.Do[i] = 1;
                IOThread_Run.DO_Trigger_olng((ushort)i, 1);
                foreach (Control ctem in groupBox2.Controls)
                {
                    if (ctem.Name == "checkBox" + (i + 1).ToString())
                    {
                        ((CheckBox)ctem).Checked = true;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)
            {
                //IOThread_Run.Do[i] = 0;
                IOThread_Run.DO_Trigger_olng((ushort)i, 0);
                foreach (Control ctem in groupBox2.Controls)
                {
                    if (ctem.Name == "checkBox" + (i + 1).ToString())
                    {
                        ((CheckBox)ctem).Checked = false;
                    }
                }

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)
            {
                foreach (Control ctem in groupBox1.Controls)
                {
                    if (ctem.Name == "textBox" + (i + 1).ToString())
                    {
                        ((TextBox)ctem).Text = IOThread_Run.Di[i].ToString();
                    }
                }

            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                IOThread_Run.DO_Trigger_olng(0, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(0, 0);
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                IOThread_Run.DO_Trigger_olng(1, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(1, 0);
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                IOThread_Run.DO_Trigger_olng(2, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(2, 0);
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                IOThread_Run.DO_Trigger_olng(3, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(3, 0);
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                IOThread_Run.DO_Trigger_olng(4, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(4, 0);
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                IOThread_Run.DO_Trigger_olng(5, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(5, 0);
            }
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                IOThread_Run.DO_Trigger_olng(6, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(6, 0);
            }
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                IOThread_Run.DO_Trigger_olng(7, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(7, 0);
            }
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                IOThread_Run.DO_Trigger_olng(8, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(8, 0);
            }
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                IOThread_Run.DO_Trigger_olng(9, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(9, 0);
            }
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox11.Checked)
            {
                IOThread_Run.DO_Trigger_olng(10, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(10, 0);
            }
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox12.Checked)
            {
                IOThread_Run.DO_Trigger_olng(11, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(11, 0);
            }
        }

        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox13.Checked)
            {
                IOThread_Run.DO_Trigger_olng(12, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(12, 0);
            }
        }

        private void checkBox14_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox14.Checked)
            {
                IOThread_Run.DO_Trigger_olng(13, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(13, 0);
            }
        }

        private void checkBox15_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox15.Checked)
            {
                IOThread_Run.DO_Trigger_olng(14, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(14, 0);
            }
        }

        private void checkBox16_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox16.Checked)
            {
                IOThread_Run.DO_Trigger_olng(15, 1);
            }
            else
            {
                IOThread_Run.DO_Trigger_olng(15, 0);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)
            {
                //IOThread_Run.Do[i] = 0;
                IOThread_Run.DO_Trigger_olng((ushort)i, 0);
                foreach (Control ctem in groupBox2.Controls)
                {
                    if (ctem.Name == "checkBox" + (i + 1).ToString())
                    {
                        ((CheckBox)ctem).Checked = false;
                    }
                }
            }

            IOThread_Run.DIORun = false;
            timer1.Stop();
            PCI1616_Initial.PCI1616_Close();
            this.Close();
        }

        private void PCI1616IO_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            OCR_Form.pCI1616IO_Form = null;
        }
    }
}
